# -*- coding: utf-8 -*-

"""
@name: configs
@author: Memory
@date: 2021/05/08
@document: 配置游戏中的一些全局设置
"""
import json
import pygame
import requests
from pathlib import Path
from random import choice
from easydict import EasyDict

'''游戏参数设置'''
MAX_TIME = 20  # 多久不操作会被计一次超时
MAX_TIMEOUT = 3  # 几次超时会被判输
MAX_NOEAT = 30  # 几步不吃子或者翻棋会被判和棋
MIN_GIVEUP = 20  # 几步以后才可以认输
WIN_CREDIT = 20  # 赢了棋加的积分
###########################################################
'''窗口设置'''
GAME_VERSION = "2.0.0"
WINDOW_TITILE = f'皇家战棋V{GAME_VERSION} By Memory'  # 窗口标题
WINDOW_SIZE = (1000, 750)  # 游戏窗口大小
###########################################################
'''位置设置。除非你很清楚你在做什么，不然不要改这些'''
ROW = 6  # 棋盘格的数量是 6*6

CHESSBOARD_POS = (200, -50)  # 棋盘偏移的位置
CHESSBOARD = [  # 定义棋盘的二位数组，每个元素是棋盘纵横线的一个交点
    [[79, 209], [153, 209], [226, 209], [300, 209], [373, 210], [446, 210], [520, 210]],
    [[71, 260], [147, 260], [223, 260], [300, 260], [376, 260], [452, 260], [528, 260]],
    [[61, 314], [141, 314], [220, 314], [300, 314], [379, 314], [458, 314], [538, 314]],
    [[51, 373], [134, 373], [217, 373], [300, 373], [382, 373], [465, 373], [548, 373]],
    [[40, 437], [127, 437], [213, 437], [300, 437], [386, 437], [472, 437], [559, 437]],
    [[28, 507], [118, 507], [209, 507], [300, 507], [390, 507], [481, 507], [571, 507]],
    [[15, 585], [110, 585], [205, 585], [300, 585], [395, 585], [490, 585], [585, 585]]
]
CHESS_SIZE = [124, 132, 140, 148, 156, 164]  # CHESS_SIZE[i]表示从上往下数第 i 层上的棋子尺寸
CHESS_POS = [  # CHESS_POS[i][j]表示当棋子处于(i ,j)位置时，棋子要blit的位置
    [[52, 124], [127, 124], [200, 124], [275, 124], [348, 124], [424, 124]],
    [[41, 169], [118, 169], [195, 169], [273, 169], [349, 169], [426, 169]],
    [[28, 218], [109, 218], [189, 218], [270, 218], [350, 218], [430, 218]],
    [[15, 275], [100, 275], [184, 275], [267, 275], [351, 275], [436, 275]],
    [[0, 336], [90, 336], [177, 336], [265, 336], [353, 336], [441, 336]],
    [[-15, 404], [77, 404], [170, 404], [262, 404], [355, 404], [448, 404]]
]
BODER = [[69, 199], [0, 600], [600, 600], [530, 200]]  # 定义棋盘边框的四个点
TIME_POS = [(45, 300), (805, 300)]  # 剩余时间所在的位置


def move_pos(pos_list):
    """
    给所有位置加上一个偏移。
    """
    if len(pos_list) == 2:
        pos_list[0] += CHESSBOARD_POS[0]
        pos_list[1] += CHESSBOARD_POS[1]
        return
    for pl in pos_list:
        move_pos(pl)


move_pos(CHESSBOARD)
move_pos(CHESS_POS)
move_pos(BODER)

DIRECTION = {pygame.K_UP: (0, -1), pygame.K_LEFT: (-1, 0), pygame.K_RIGHT: (1, 0), pygame.K_DOWN: (0, 1), '0': (0, 0)}
MOVE_DIRE = {(0, -1): 0, (0, 1): 1, (-1, 0): 2, (1, 0): 3}
###########################################################
'''颜色设置'''
COLOR = {
    'boder': '0xFFD04B',  # 棋盘边框的颜色
    'board_1': '0xAA8FC3',  # 棋盘格颜色
    'board_2': '0xFFFFFF',  # 棋盘格颜色
    'hover': '0x22EE22',  # 鼠标悬停时棋盘格的颜色
    'select': '0xFF9900',  # 棋子被选中时棋盘格的颜色
    'next': '0xB0E2FF',  # 被选中的棋子下一步可走的棋盘格颜色
    'last_step': '0x9AFF9A',  # 上一步翻开或者移动的棋子位置
    'time_text': '0xCD4F39'  # 时间字体的颜色
}
for k, v in COLOR.items():  # 转换成pyagme里能用的格式
    COLOR[k] = pygame.color.Color(v)
COLOR = EasyDict(COLOR)
###########################################################
'''路径设置'''
def get_files(path: Path):
    files = {}
    for file in path.iterdir():
        files[file.stem] = file.as_posix()
    return EasyDict(files)


ROOT_PATH = Path(__file__).parent           # 获取文件所在目录的绝对路径
IMG_PATH = ROOT_PATH / 'src' / 'image'      # 图像的路径
SOUND_PATH = ROOT_PATH / 'src' / 'sound'    # 声音的路径
ALL_IMG = get_files(IMG_PATH)               # 存储所有图像名与路径的键值对
ALL_SOUND = get_files(SOUND_PATH)           # 存储所有声音名与路径的键值对

CLIENT_LOG_PATH = ROOT_PATH / 'log'
###########################################################

'''随机用户设置'''
# 随机的姓
XING = '赵钱孙李周吴郑王冯陈褚卫蒋沈韩杨朱秦尤许何吕施张孔曹严华金魏陶姜戚谢邹喻柏水窦章云苏潘葛' \
       '奚范彭郎鲁韦昌马苗凤花方俞任袁柳酆鲍史唐费廉岑薛雷贺倪汤滕殷罗毕郝邬安常乐于时傅皮卞齐康' \
       '伍余元卜顾孟平黄和穆萧尹姚邵湛汪祁毛禹狄米贝明臧计伏成戴谈宋茅庞熊纪舒屈项祝董梁杜阮蓝闵' \
       '席季麻强贾路娄危江童颜郭梅盛林刁钟徐邱骆高夏蔡田樊胡凌霍虞万支柯昝管卢莫经房裘缪干解应宗' \
       '丁宣贲邓郁单杭洪包诸左石崔吉钮龚程嵇邢滑裴陆荣翁荀羊於惠甄曲家封芮羿储靳汲邴糜松井段富巫' \
       '乌焦巴弓牧隗山谷车侯宓蓬全郗班仰秋仲伊宫宁仇栾暴甘钭厉戎祖武符刘景詹束龙叶幸司韶郜黎蓟薄' \
       '印宿白怀蒲邰从鄂索咸籍赖卓蔺屠蒙池乔阴鬱胥能苍双闻莘党翟谭贡劳逄姬申扶堵冉宰郦雍卻璩桑桂' \
       '濮牛寿通边扈燕冀郏浦尚农温别庄晏柴瞿阎充慕连茹习宦艾鱼容向古易慎戈廖庾终暨居衡步都耿满弘' \
       '匡国文寇广禄阙东欧殳沃利蔚越夔隆师巩厍聂晁勾敖融冷訾辛阚那简饶空曾毋沙乜养鞠须丰巢关蒯相' \
       '查后荆红游竺权逯盖益桓公万俟司马上官欧阳夏侯诸葛闻人东方赫连皇甫尉迟公羊澹台公冶宗政濮阳' \
       '淳于单于太叔申屠公孙仲孙轩辕令狐钟离宇文长孙慕容鲜于闾丘司徒司空丌官司寇仉督子车颛孙端木' \
       '巫马公西漆雕乐正壤驷公良拓跋夹谷宰父谷梁晋楚闫法汝鄢涂钦段干百里东郭南门呼延归海羊舌微生' \
       '岳帅缑亢况郈有琴梁丘左丘东门西门商牟佘佴伯赏南宫墨哈谯笪年爱阳佟第五言福'
# 随机的名
MING = '伟刚勇毅俊峰强军平保东文辉力明永健世广志义兴良海山仁波宁贵福生龙元全国胜学祥才发武新利清' \
       '飞彬富顺信子杰涛昌成康星光天达安岩中茂进林有坚和彪博诚先敬震振壮会思群豪心邦承乐绍功松善' \
       '厚庆磊民友裕河哲江超浩亮政谦亨奇固之轮翰朗伯宏言若鸣朋斌梁栋维启克伦翔旭鹏泽晨辰士以建家' \
       '致树炎德行时泰盛秀娟英华慧巧美娜静淑惠珠翠雅芝玉萍红娥玲芬芳燕彩春菊兰凤洁梅琳素云莲真环' \
       '雪荣爱妹霞香月莺媛艳瑞凡佳嘉琼勤珍贞莉桂娣叶璧璐娅琦晶妍茜秋珊莎锦黛青倩婷姣婉娴瑾颖露瑶' \
       '怡婵雁蓓纨仪荷丹蓉眉君琴蕊薇菁梦岚苑筠柔竹霭凝晓欢霄枫芸菲寒欣滢伊亚宜可姬舒影荔枝思丽秀' \
       '飘育馥琦晶妍茜秋珊莎锦黛青倩婷宁蓓纨苑婕馨瑗琰韵融园艺咏卿聪澜纯毓悦昭冰爽琬茗羽希'
# 战棋中积分和称号
TITLE = {'平民': (0, 40), '小农场主': (40, 100), '大农场主': (100, 200), '贵族': (200, 360),
         '小城主': (360, 760), '大城主': (760, 1400), '男爵': (1400, 2600), '子爵': (2600, 4400),
         '伯爵': (4400, 7600), '侯爵': (7600, 15000), '公爵': (15000, 33000), '国王': (33000, 65000),
         '教皇': (65000, 999999)}
###########################################################
"""requests伪装"""
USER_AGENT = [
    "Mozilla/5.0 (compatible; Baiduspider/2.0; +http://www.baidu.com/search/spider.html)",
    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; AcooBrowser; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
    "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; Acoo Browser; SLCC1; .NET CLR 2.0.50727; Media Center PC 5.0; .NET CLR 3.0.04506)",
    "Mozilla/4.0 (compatible; MSIE 7.0; AOL 9.5; AOLBuild 4337.35; Windows NT 5.1; .NET CLR 1.1.4322; .NET CLR 2.0.50727)",
    "Mozilla/5.0 (Windows; U; MSIE 9.0; Windows NT 9.0; en-US)",
    "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Win64; x64; Trident/5.0; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 2.0.50727; Media Center PC 6.0)",
    "Mozilla/5.0 (compatible; MSIE 8.0; Windows NT 6.0; Trident/4.0; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET CLR 1.0.3705; .NET CLR 1.1.4322)",
    "Mozilla/4.0 (compatible; MSIE 7.0b; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.04506.30)",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN) AppleWebKit/523.15 (KHTML, like Gecko, Safari/419.3) Arora/0.3 (Change: 287 c9dfb30)",
    "Mozilla/5.0 (X11; U; Linux; en-US) AppleWebKit/527+ (KHTML, like Gecko, Safari/419.3) Arora/0.6",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.2pre) Gecko/20070215 K-Ninja/2.1.1",
    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9) Gecko/20080705 Firefox/3.0 Kapiko/3.0",
    "Mozilla/5.0 (X11; Linux i686; U;) Gecko/20070322 Kazehakase/0.4.5",
    "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.8) Gecko Fedora/1.9.0.8-1.fc10 Kazehakase/0.5.6",
    "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.56 Safari/535.11",
    "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/535.20 (KHTML, like Gecko) Chrome/19.0.1036.7 Safari/535.20",
    "Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; fr) Presto/2.9.168 Version/11.52"
]

# 浏览器头部
HEADERS = {
    'User-Agent': choice(USER_AGENT),
    'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
    'Accept-Language': 'en-US,en;q=0.5',
    'Connection': 'keep-alive',
}

'''IP 设置，联网对战的服务器'''
HOST = 'hjzq.xwysj.com'  # 服务器地址
PORT = 1122  # 端口
LOCAL_HOST = '127.0.0.1'  # 本地服务器地址
LOCAL_PORT = 1122  # 本地端口
WEB_CONFIG_URL = "https://gitee.com/MemoryD/BattleChessServer/raw/master/web_config.json"


def get_web_config(timeout=10):
    try:
        res = requests.get(WEB_CONFIG_URL, headers=HEADERS, timeout=timeout)
        return EasyDict(json.loads(res.text))
    except:
        return None


config = get_web_config()
MESSAGE = ""
UPDATE = None
if config:
    MESSAGE = config.message
    if config.server.enable:
        HOST = config.server.host
        PORT = config.server.port
    if config.update.enable:
        UPDATE = config.update
